<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use App\Http\Org\Categoryclass;

class Category extends Model
{
    protected $table = 'category';
    protected $primaryKey = 'cate_id';
    public $timestamps = false;
    /**
     * 不能被批量赋值的属性
     *
     * @var array
     */
    protected $guarded = [];


    //返回分类栏目列表
    public function cate_list()
    {
        return $this->orderBy('cate_order','asc')->get();
    }

    //返回排序后的栏目：适用于列表页面
    public function get_tree()
    {
        $data = $this->cate_list(); //所有栏目
        //return Categoryclass::catesort($data);      //适用更广，0.053003072738647
        return Categoryclass::cate_foreach($data); //速度更快，0.044002056121826
    }

    /**
     * 一对多关联模型 关联article 表的 cate_id ('Category', '外键', '本身的字段')
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function cate_article($cate_id)
    {
        $str = ['art_id','art_title','art_description','art_thumb','art_time','art_editor','art_view','cate_id','art_order'];
        return $this->hasOne('App\Http\Model\Article', 'cate_id', 'cate_id')->select();
    }

}
